package support;

import machine.WashingMachine;

public class WashingCycle implements iWashingCycle {

	Program program;
	WashingMachine washingMachine;
	private int timeElapsed = 0;
	private int time;

	WashingCycle(WashingMachine washingMachine) {
		this.washingMachine = washingMachine;
		program = washingMachine.getProgramator().getSelectedProgram();
		time = washingMachine.getProgramator().getTime();
	}

	@Override
	public void washing() {
		System.out.println("--------STARTING WASHING--------");
		washingMachine.startWashing();

		while (timeElapsed < time/3) {
			System.out.println("Washing!. Time elapsed: "
					+ timeElapsed);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			timeElapsed++;
		}
		washingMachine.stopWashing();
	}

	@Override
	public void rinsing() {
		System.out.println("-------STARTING RINSING-------");
		washingMachine.startRinsing();

		while (timeElapsed < (time * 2) / 3) {
			System.out.println("Rinsing!. Time elapsed: "
					+ (timeElapsed));
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			timeElapsed++;
		}
		washingMachine.stopRinsing();

	}

	@Override
	public void spinning() {
		System.out.println("--------STARTING SPINING--------");
		washingMachine.startSpinning();

		while (timeElapsed <= time) {
			System.out.println("Spining!. Time elapsed: "
					+ (timeElapsed));
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			timeElapsed++;
		}

	}

	public void startCycle() {
		washing();
		rinsing();
		spinning();
	}

}
